﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Input;
using System.Windows.Threading;

namespace NapThe
{
    public partial class NapTheDienThoai : UserControl, INapTheDienThoai
    {
        public DispatcherTimer newTimer = new DispatcherTimer();
        public static string maTheDienThoai = "";
        public static string maSeRi = "";
        Grid MainLayoutRoot;
        NapXu napxu;
        public NapTheDienThoai(Grid MainLayoutRoot, NapXu napxu)
        {
            InitializeComponent();
            this.MainLayoutRoot = MainLayoutRoot;
            this.MainLayoutRoot.Children.Add(this);
            this.napxu = napxu;
        }

     
        public void OnVisible()
        {
            this.Visibility = Visibility.Visible;
            
            rectangle1.Width = 0;
            textBlock8.Text = "";
            newTimer.Tick -= OnTimerTick;
            //newTimer.Start();
            newTimer.Interval = TimeSpan.FromMilliseconds(150);
            newTimer.Tick += OnTimerTick;
            if (NapTheDienThoaiController.isRunning)
            {
                this.tbMaSo.Text = NapTheDienThoaiController.tbMaSo;
                newTimer.Start();
                this.tbSeRi.Text = NapTheDienThoaiController.tbSeri;
            }
            else
            {
                if (!"".Equals(maTheDienThoai))
                {
                    tbMaSo.Text = maTheDienThoai;
                }
                if (!"".Equals(maSeRi))
                {
                    tbSeRi.Text = maSeRi;
                }
            }
            switch (NapTheDienThoaiModel.intNhaMang)
            {
                case 1: this.radioButton1.IsChecked = true; break;
                case 2: this.radioButton2.IsChecked = true; break;
                case 3: this.radioButton3.IsChecked = true; break;
            }

        }
        public void OnCollapsed()
        {
            this.Visibility = Visibility.Collapsed;
           
            newTimer.Tick -= OnTimerTick;
            newTimer.Stop();
        }
        public void OnTimerTick(object sender, EventArgs e)
        {
            if (NapTheDienThoaiController.counter >= 100)
            {
                newTimer.Stop();
                //IEnumerable<BackgroundTransferRequest> a = BackgroundTransferService.Requests;
                //if (a != null)
                //{
                //    foreach (var request in a)
                //    {
                //        Controller.DownloadController.RemoveTransferRequest(request.RequestId);
                //    }
                //}

                NapTheDienThoaiController.counter = 0;
                NapTheDienThoaiController.isRunning = false;
                //Controller.DownloadController.count = 1;
                MessageBox.Show("Tạm thời không kết nối được mạng Internet!");
            }
            else
            {
                textBlock8.Text = NapTheDienThoaiController.counter + "% completed";
                rectangle1.Width = (double)(4 * NapTheDienThoaiController.counter);
                NapTheDienThoaiController.counter++;
            }
        }
        public void Click_ChangeToNapTheQuaTang(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
            
            this.OnCollapsed();
            napxu.naptheQplay.OnVisible();
           
        }
        private void textWhenMouseLeftDown(object sender, MouseButtonEventArgs e)
        {
            if (tbMaSo.Text.Equals("Nhập mã số thẻ cào")
                || tbMaSo.Text.ToUpper().Contains("NHẬP MÃ SỐ THẺ CÀO")
                )
            {
                tbMaSo.Text = "";
            }
        }
        private void textWhenMouseLeftDown1(object sender, MouseButtonEventArgs e)
        {
            if (tbSeRi.Text.Equals("Nhập số seri in trên thẻ")
                || tbSeRi.Text.ToUpper().Contains("NHẬP SỐ SERI IN")
                )
            {
                tbSeRi.Text = "";
            }
        }
        private void DownClick(object sender, MouseEventArgs e)
        {
            TextBlock t = (TextBlock)sender;
            this.setResource(t.Name, "BGStyle4", "BGStyle3");
        }
        private void btnKhong_Click(object sender, RoutedEventArgs e)
        {
            if (!NapTheDienThoaiController.isRunning
                //&& !Controller.NapTheQuaTangController.isRunning
                   )
            {
                setResource("textBlock26", "BGStyle3", "BGStyle3");
                //  NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.RelativeOrAbsolute));
                OnCollapsed();
            }
            else
            {
                MessageBox.Show("Hệ thống đang nạp thẻ cào");
            }
                
        }
        private void btnNapThe_Click(object sender, RoutedEventArgs e)
        {
            if (!NapTheDienThoaiController.isRunning
                //&& !Controller.NapTheQuaTangController.isRunning
             )
            {
              
                setResource("textBlock24", "BGStyle3", "BGStyle3");
                NapTheDienThoaiController n = new NapTheDienThoaiController();
                n.process(this);
            }
            else
            {
                MessageBox.Show("Hệ thống đang nạp thẻ cào");
            }
              
        }
        public void setResource(string name, string source1, string source2)
        {
            if (!source1.Equals(source2))
            {
                resetResource(name, source2);
            }
            if (button12.Name.Equals(name))
            {
                button12.Style = (Style)this.Resources[source1];
                return;
            }
            if (button13.Name.Equals(name))
            {
                button13.Style = (Style)this.Resources[source1];
                return;
            }
            if (button14.Name.Equals(name))
            {
                button14.Style = (Style)this.Resources[source1];
                return;
            }

            if (textBlock26.Name.Equals(name))
            {
                button14.Style = (Style)this.Resources[source1];
                return;
            }
            if (textBlock25.Name.Equals(name))
            {
                button13.Style = (Style)this.Resources[source1];
                return;
            }
            if (textBlock24.Name.Equals(name))
            {
                button12.Style = (Style)this.Resources[source1];
                return;
            }
        }
        private void resetResource(string name, string source)
        {
            if (!textBlock26.Name.Equals(name))
            {
                button14.Style = (Style)this.Resources[source];
                //return;
            }
            if (!textBlock25.Name.Equals(name))
            {
                button13.Style = (Style)this.Resources[source];
                //return;
            }
            if (!textBlock24.Name.Equals(name))
            {
                button12.Style = (Style)this.Resources[source];
                //return;
            }

        }
        private void btnKhac_Click(object sender, RoutedEventArgs e)
        {
            if (!NapTheDienThoaiController.isRunning
                //&& !Controller.NapTheQuaTangController.isRunning
                )
            {
                setResource("textBlock25", "BGStyle3", "BGStyle3");
                OnCollapsed();
                napxu.khacScreen.OnVisible();
            }
            else
            {
                MessageBox.Show("Hệ thống đang nạp thẻ cào");
            }
            
                
          
        }

        private void textBlock26_MouseLeave(object sender, MouseEventArgs e)
        {
            setResource("textBlock26", "BGStyle3", "BGStyle3");
        }

        public void setRectangle1(double x)
        {
            this.rectangle1.Width = x;
        }

        public void setTextBlock(string name, string content)
        {
            if (textBlock8.Name.Equals(name))
            {
                textBlock8.Text = content;
            }
        }

        public string getMaSo()
        {
            return tbMaSo.Text;
        }

        public string getSeri()
        {
            return tbSeRi.Text;
        }

        public bool getRadio(string name)
        {
            if (this.radioButton1.Name.Contains(name))
            {
                return Convert.ToBoolean(this.radioButton1.IsChecked);
            }
            if (this.radioButton2.Name.Contains(name))
            {
                return Convert.ToBoolean(this.radioButton2.IsChecked);
            }

            return Convert.ToBoolean(this.radioButton3.IsChecked);
        }

        public void processTimer(string nameOfAction)
        {
            if ("Start".Equals(nameOfAction))
            {
                this.newTimer.Start();
            }
            if ("Stop".Equals(nameOfAction))
            {
                this.newTimer.Stop();
            }
        }

        public void navigateWhenSuccess()
        {
            this.Visibility = Visibility.Collapsed;
            OnCollapsed();
           
        }

        public void navigateWhenFailed()
        {
            NapXu.report.OnNavigatedTo();
        }
    }
}
